﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using trangbaocao.Models;
using System.Collections;

namespace trangbaocao.Controllers
{
    [Authorize]
    public class Device_StationsController : Controller
    {
        //
        // GET: /Device_Stations/

        Device_StationsModel Device_StationsModel = new Device_StationsModel();
        StationsModel StationsModel = new StationsModel();
        DeviceModel DeviceModel = new DeviceModel();
        Main_StationsModel Main_StationsModel = new Main_StationsModel();
        public ActionResult Index()
        {
            Device_StationsAssociateModel Device_StationsAssociateModel = new Device_StationsAssociateModel();
            List<Device_StationsAssociateModel> Device_Stations = Device_StationsAssociateModel.getDevice_StationsIndex();

            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");

            //List<StationsModel> stationModel = StationsModel.getStations();
            //ViewBag.tram_id = new SelectList(stationModel, "id", "name");

            return View(Device_Stations);
        }
        public ActionResult AddDevice_Stations()
        {   
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            Device_StationsModel.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            
            //List<StationsModel> stationModel = StationsModel.getStations();
            List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Convert.ToInt32(Device_StationsModel.trungtam_id.Skip(0).First().Value));
            Device_StationsModel.tram_id = new SelectList(stationModel, "id", "name");

            List<DeviceModel> deviceModel = DeviceModel.getDevice();
            Device_StationsModel.thietbi_id = new SelectList(deviceModel, "id", "name");

            return View(Device_StationsModel);
        }
        [HttpPost]
        public ActionResult AddDevice_Stations(Device_StationsModel Device_Stations)
        {
            List<DeviceModel> deviceModel = DeviceModel.getDevice();
            //List<StationsModel> stationModel = StationsModel.getStations();            
            List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Device_Stations.main_stations_id);
            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            if (ModelState.IsValid)
            {
                if (Device_StationsModel.checkExist(Device_Stations) > 0 || Device_StationsModel.checkExist_Ma_he_thong(Device_Stations)>0)
                {
                    TempData["msg"] = "Tên thiết bị hoặc mã hệ thống đã tồn tại!";
                    Device_Stations.trungtam_id = new SelectList(main_StationsModel, "id", "name");
                    Device_Stations.tram_id = new SelectList(stationModel, "id", "name");
                    Device_Stations.thietbi_id = new SelectList(deviceModel, "id", "name");
                    return View(Device_Stations);
                }
                Device_Stations.trang_thai_hd = Convert.ToInt16(Request["trang_thai_hd"]);
                Device_StationsModel.addDevice_Stations(Device_Stations);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("Index");
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";
            Device_Stations.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            Device_Stations.tram_id = new SelectList(stationModel, "id", "name");
            Device_Stations.thietbi_id = new SelectList(deviceModel, "id", "name");
            return View(Device_Stations);
        }
        public ActionResult EditDevice_Stations(int id)
        {
            Device_StationsModel Device_StationsModel_ = Device_StationsModel.getDevice_Stations().Find(delegate(Device_StationsModel s) { return s.id == id; });

            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            Device_StationsModel_.trungtam_id = new SelectList(main_StationsModel, "id", "name");

            List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Device_StationsModel_.main_stations_id);
            Device_StationsModel_.tram_id = new SelectList(stationModel, "id", "name");

            List<DeviceModel> deviceModel = DeviceModel.getDevice();
            Device_StationsModel_.thietbi_id = new SelectList(deviceModel, "id", "name");
            return View(Device_StationsModel_);
        }
        [HttpPost]
        public ActionResult EditDevice_Stations(Device_StationsModel Device_Stations)
        {
            if (ModelState.IsValid)
            {
                Device_Stations.trang_thai_hd = Convert.ToInt16(Request["trang_thai_hd"]);
                Device_StationsModel.updateDevice_Stations(Device_Stations);
                TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
                return RedirectToAction("Index");
            }
            TempData["msg"] = "Bạn đã nhập sai thông tin!";

            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();
            Device_Stations.trungtam_id = new SelectList(main_StationsModel, "id", "name");

            List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Device_Stations.main_stations_id);
            Device_Stations.tram_id = new SelectList(stationModel, "id", "name");

            List<DeviceModel> deviceModel = DeviceModel.getDevice();
            Device_Stations.thietbi_id = new SelectList(deviceModel, "id", "name");
            return View(Device_Stations);
        }

        public ActionResult DeleteDevice_Stations(int id)
        {
            if (Device_StationsModel.checkSubId_Fuel(id) > 0 || Device_StationsModel.checkSubId_Fuel_Detail(id)>0)
            {
                TempData["error"] = "Bạn không thể xóa do dữ liệu này đã được sử dụng!";
                return RedirectToAction("Index");
            }
            Device_StationsModel.deleteDevice_Stations(id);
            TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult CheckedAction(int[] Items)
        {
            foreach (int item in Items)
            {
                if (Device_StationsModel.checkSubId_Fuel(item) > 0 || Device_StationsModel.checkSubId_Fuel_Detail(item) > 0)
                {
                    TempData["error"] = "Bạn không thể xóa do có dữ liệu đã chọn đã được sử dụng!";
                    return RedirectToAction("Index");
                }
            }
            foreach (int item in Items)
            {
                Device_StationsModel.deleteDevice_Stations(item);
            }
            TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            return RedirectToAction("Index");
        }
        public ActionResult FindDevice_Stations()
        {
            return RedirectToAction("Index");
        }
        [HttpPost]
        public ActionResult FindDevice_Stations(string timkiem)
        {
            string trungtam_id = "";
            string tram_id = "";
            int num;
            bool main_stations_id_check = int.TryParse(trungtam_id,out num);
            string s = "";
            //add by Namtd 09/03/2014
            ViewBag.selectedMain_stations = "";
            ViewBag.selectedStations = "";
            if (int.TryParse(Request.Form["main_stations_id"],out num))
            {
                trungtam_id = Request.Form["main_stations_id"];
                s = " d.id=" + trungtam_id + " AND ";
                //add by Namtd 09/03/2014
                ViewBag.selectedMain_stations = Request.Form["main_stations_id"];
            }
            if (int.TryParse(Request.Form["stations_id"], out num))
            {
                tram_id = Request.Form["stations_id"];
                s = " b.id=" + tram_id + " AND ";
                //add by Namtd 09/03/2014
                ViewBag.selectedStations = Request.Form["stations_id"];
            }
            if (int.TryParse(Request.Form["main_stations_id"], out num) && int.TryParse(Request.Form["stations_id"], out num))
                s = " d.id=" + trungtam_id + " AND b.id=" + tram_id+" AND ";
            if (Session != null && s.ToString()=="" )
            {
                int quyen_id = Convert.ToInt16(Session["quyen_id"]);
                int trungtam_session_id = Convert.ToInt16(Session["trungtam_id"]);
                if (quyen_id > 2)
                    s = " d.id="+trungtam_session_id+" AND ";
            }
            
            string sql;
            string dk = "";
            if (Request["select_option"] == "device_stations.name")
                dk = "a.name";
            else if (Request["select_option"] == "device_stations.ma_he_thong")
                dk = "a.ma_he_thong";
            else if (Request["select_option"] == "device.name")
                dk = "c.name";
            //else if (Request["select_option"] == "stations.name")
            //    dk = "b.name";
            //else if (Request["select_option"] == "main_stations.name")
            //    dk = "d.name";
            else if (Request["select_option"] == "sort_device.name")
                dk = "e.name";
            else if (Request["select_option"] == "main_sort_device.name")
                dk = "f.name";
            if (timkiem == "")
                sql = s + "("+ dk + " like '%" + timkiem + "%' or " + dk + " is null)";
            else
                sql = s + "(" + dk + " like '%" + timkiem + "%' or upper(" + dk + ") like upper('%" + timkiem + "%'))";

            

            ViewBag.find_textbox = timkiem;
            ViewBag.find_selectbox = Request["select_option"];
            Device_StationsAssociateModel Device_StationsAssociateModel = new Device_StationsAssociateModel();
            List<Device_StationsAssociateModel> Device_Stations = Device_StationsAssociateModel.FindDevice_Stations(sql);

            List<Main_StationsModel> main_StationsModel = Main_StationsModel.getMain_Stations();

            //add by Namtd 09/03/2014
            if (ViewBag.selectedMain_stations.Length == 0)
                ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name");
            else
            {
                ViewBag.trungtam_id = new SelectList(main_StationsModel, "id", "name", ViewBag.selectedMain_stations);
                List<StationsModel> stationModel = StationsModel.getStationsByMain_Stations(Convert.ToInt32(ViewBag.selectedMain_stations));
                ViewBag.tram_id = new SelectList(stationModel, "id", "name", ViewBag.selectedStations);
            }
            return View("Index",Device_Stations);
        }
        
        [HttpPost]
        public JsonResult getStationsJson(string MainStationId, string Order )
        {
            return Json(Device_StationsModel.getStations(MainStationId,Order));
        }
        public void LoadStationsCombobox(int id)
        {
            Device_StationsModel Device_Stations = new Device_StationsModel();
            List<Main_StationsModel> device_stationsModel = Device_Stations.getMain_StationsById(id);
            Device_Stations.tram_id = new SelectList(device_stationsModel, "id", "name");
        } 
        public ActionResult Device_Stations_Report_Index()
        {
            return View("~/Views/Device_Stations/Report/Device_Stations_Report_Index.cshtml");
        }
        public ActionResult Re_Device_Stations_Sum()
        {
            return View("~/Views/Device_Stations/Report/Re_Device_Stations_Sum.cshtml");
        }
    }
}
